package com.dense.kuiniu.floatcabinet.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.dense.kuiniu.floatcabinet.R;
import com.dense.kuiniu.floatcabinet.mvp.event.BorrowBackResultEvent;
import com.dense.kuiniu.floatcabinet.mvp.event.DoorOpenOrCloseEvent;
import com.dense.kuiniu.floatcabinet.mvp.event.OpenDoorEvent;
import com.dense.kuiniu.floatcabinet.mvp.event.ScanEvent;
import com.dense.kuiniu.floatcabinet.mvp.event.TaskInfoEvent;
import com.dense.kuiniu.floatcabinet.mvp.manager.bord.Door;
import com.dense.kuiniu.floatcabinet.mvp.manager.bord.DoorManager;
import com.dense.kuiniu.floatcabinet.mvp.ui.adapter.BoxForReaderAdapter;
import com.dense.kuiniu.floatcabinet.mvp.ui.view.DialogDoorNotClose;
import com.dense.kuiniu.floatcabinet.mvp.ui.view.DialogDooroOpened;
import com.dense.kuiniu.floatcabinet.mvp.ui.view.DialogMessage;
import com.dense.kuiniu.floatcabinet.utils.timer.CountDownView;
import com.dense.kuiniu.floatcabinet.utils.timer.CounterController;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.EventBusManager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 * @date 2020/9/29
 */
public class BorrowActivity extends ImmerseActivity {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.view_count_down)
    CountDownView mViewCountDown;
    private BoxForReaderAdapter boxAdapter;
    private DialogMessage mDialogMessage;
    private DialogDoorNotClose dialogDoorNotClose;
    private DialogDooroOpened dialogDooroOpened;
    private Door mCurrentOpenDoor;
    private String cardNo;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

    }

    @OnClick({R.id.tv_back, R.id.iv_search})
    public void onClickView(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.iv_search:
                break;
            default:
                break;
        }
    }


    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_borrow;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mRecyclerView.post(() -> {
            mDialogMessage = new DialogMessage(BorrowActivity.this);
            dialogDoorNotClose = new DialogDoorNotClose(BorrowActivity.this);
            dialogDooroOpened = new DialogDooroOpened(BorrowActivity.this);
            Intent intent = getIntent();
            cardNo = intent.getStringExtra("cardNo");

            StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
            mRecyclerView.setLayoutManager(manager);
            List<Door> list = DoorManager.getInstance().getAllDoorList();
            list.add(1, new Door(-1) {
                @Override
                public int getItemType() {
                    return 2;
                }
            });
            mRecyclerView.setAdapter(boxAdapter = new BoxForReaderAdapter(list));
            boxAdapter.addChildClickViewIds(R.id.layout_container);
            boxAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                @Override
                public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                    Door door = (Door) adapter.getItem(position);
                    if (door != null) {
                        if (checkDoors()) {
                            EventBusManager.getInstance().post(new OpenDoorEvent(cardNo, 1, door.getDoorNumber()));
                            mCurrentOpenDoor = door;
                        }
                    }
                }
            });
            mViewCountDown.start(new CounterController.OnCompleteListener() {
                @Override
                public void onComplete() {
                    finish();
                }
            });
        });

    }


    public void showInfo(String msg) {
        mDialogMessage.justInfo(msg).show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void taskInfoEvent(TaskInfoEvent event) {
        showInfo("门号:" + event.doorNumber + "\n动作:" + event.action);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void borrowBackEvent(BorrowBackResultEvent resultEvent) {
        Intent intent = new Intent(this, BorrowCompleteActivity.class);
        intent.putExtra("result", resultEvent.result);
        startActivity(intent);
        mDialogMessage.dismiss();
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void doorStateEvent(DoorOpenOrCloseEvent event) {
        List<Door> openDoorList = DoorManager.getInstance().getOpenDoorList();
        if (!openDoorList.isEmpty()) {
            dialogDooroOpened.updateInfo(openDoorList);
            dialogDooroOpened.show();
        }

        if (boxAdapter != null) {
            boxAdapter.notifyDataSetChanged();
        }
        if (mCurrentOpenDoor != null && !mCurrentOpenDoor.isOpen()) {
            EventBusManager.getInstance().post(new ScanEvent(cardNo, mCurrentOpenDoor.getDoorNumber(), 1));
        }
    }

    private boolean checkDoors() {
        List<Door> openDoors = DoorManager.getInstance().getOpenDoorList();
        if (!openDoors.isEmpty()) {
            dialogDoorNotClose.updateInfo(openDoors);
            dialogDoorNotClose.show();
            return false;
        }
        return true;
    }
}